package com.chencai.wiki;

import java.io.StringWriter;
import java.io.Writer;

import be.devijver.wikipedia.Parser;
import be.devijver.wikipedia.SmartLinkResolver;
import be.devijver.wikipedia.Visitor;
import be.devijver.wikipedia.html.HtmlVisitor;

public class WikiParser 
{
	private static class MyHtmlVisitor extends HtmlVisitor 
	{
		   public MyHtmlVisitor(Writer out, SmartLinkResolver resolver) {
			      super(out, resolver);
			   }
		   
		   @Override
		   public void startLiteral() 
		   {
               output.append("<p>\n");
		   }
		   
		   @Override
		   public void endLiteral()
		   {
			   output.append("</p>");
		   }

		   
		   public void handleSmartLinkWithoutCaption(String s)
		   {
			   String content = "<a href=\"#\" onclick=\"goTo('" +  s.trim() + "');\">" + s.trim() + "</a>";
			   output.append(content);
			   output.flush();
		   }
		   
		   public void startSmartLinkWithCaption(String s)
		   {
			   String content = "<a href=\"#\" onclick=\"goTo('" +  s.trim() + "');\">";
			   output.append(content);
			   output.flush();
		   }

	}
	
	public static String toHtml(String wikiText)
	{
		
		String ret="";
		SmartLinkResolver smartLinkResolver =
			   new SmartLinkResolver() 
				{
			      public String resolve(String smartLink) {
			         
			          return "#";		         
			      }
				};
		
		StringWriter writer = new StringWriter();
		Visitor visitor = new MyHtmlVisitor(writer, smartLinkResolver);
		Parser.withVisitor(wikiText, visitor);
		ret = writer.toString();
		return ret;
	}
	
	public static void main(String[] args)
	{
		String wiki = "test\n==title==[[test is]]";
		System.out.println(toHtml(wiki));
	}

}
